package com.effort.com.effort.excle.utils;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;

import java.util.Date;
import java.util.List;

/**
 * 日期工具类,帮助实现日期的分块
 */
public class DateTimeExcleUtils {

    private static Date parse(String date){
        return DateUtil.parse(date,"yyyy-MM-dd hh:mm:ss");
    }

    private static String format(Date date){
        return DateUtil.format(date,"yyyy-MM-dd hh:mm:ss");
    }

    /**
     * 从开始日期到结束日期,将所有的日期进行分块处理
     */
    public static List<ExcleExportDate> transform(String startDate,String endDate){


        List<ExcleExportDate> result = Lists.newArrayList();

        ExcleExportDate bean = null;

        while (null != (bean = fill(startDate,endDate))){
            result.add(bean);
            startDate = format(DateUtil.offsetDay(parse(startDate),1));
        }

        return result;
    }

    // 返回第一个合适的日期,偏移量现在为1天
    public static ExcleExportDate fill(String startDate,String endDate){
        Date start = parse(startDate);
        Date end = parse(endDate);

        if(parse(startDate).getTime() > parse(endDate).getTime()){
            return null;
        }

        if (DateUtil.offsetDay(parse(startDate),1).getTime() < parse(endDate).getTime() ){
            return new  ExcleExportDate(startDate,format(DateUtil.offsetDay(parse(startDate),1)));
        }else{
            return new ExcleExportDate(startDate,endDate);
        }
    }

    public static void main(String[] args) {
        /// 测试日期格式是否正确
        List<ExcleExportDate> transform = transform("2019-08-11 12:00:00", "2019-09-11 23:00:00");
        System.out.println(transform);
    }

}
